<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;

class ClassoddController extends Controller
{
    public function index()
    {
    	$one = DB::table('classone')->get();
        $odd = DB::table('classodd')->get();
        $even = DB::table('classeven')->get();
        return view('admin.classone.index',['one'=>$one,'odd'=>$odd,'even'=>$even]);
    }
     public function destroy($id)
    {
    	// dd($id);
    	$m = DB::table('classodd')->where('oddid',$id)->delete();
    	// dd($m);
    	if ($m>0) {
    		return redirect('/admin/classone');
    	}
    }
     public function edit($id)
    {
        $odd = DB::table('classodd')->where('oddid',$id)->first();
        return view('admin.classodd.edit',['odd'=>$odd]);
    }
     public function update($id,Request $request)
    {
        $class = DB::table('classodd')->where('oddid',$id)->get();
        $classname = $class[0]->classname;
        $goodsname = $request->input('classname');
        $goods = DB::table('goods')->get();
        foreach ($goods as $k => $v) {
            if ($classname == $v->goodsname ) {
                $goodsid = $v->goodsid;
                $goodsda = array('goodsname'=>$goodsname);
                DB::table('goods')->where('goodsid',$goodsid)->update($goodsda);
            }
        }
        $one = DB::table('classone')->get();
        foreach ($one as $k => $v) {
            if ($classname == $v->classname ) {
                $oneid = $v->oneid;
                $oneda = array('classname'=>$goodsname);
                DB::table('classone')->where('oneid',$oneid)->update($oneda);
            }
        }
        $even = DB::table('classeven')->get();
        foreach ($even as $k => $v) {
            if ($classname == $v->classname ) {
                $evenid = $v->evenid;
                $evenda = array('classname'=>$goodsname);
                DB::table('classeven')->where('evenid',$evenid)->update($evenda);
            }
        }
        // 修改单行分类表里面名字相同的字段
        $odd = DB::table('classodd')->get();
        foreach ($odd as $k => $v) {
            if ($classname == $v->classname ) {
                $oddid = $v->oddid;
                $oddda = array('classname'=>$goodsname);
                DB::table('classodd')->where('oddid',$oddid)->update($oddda);
            }
        }
        $data = $request->only('classname');
        $m = DB::table('classodd')->where('oddid',$id)->update($data);
        if($m>0){
            return redirect('/admin/classone');
        }else{
            return redirect('/admin/classone');
        }
    }
    public function create()
    {
        return view('admin.classodd.add');

    }
    public function ajax()
    {
        // echo $_GET['cid'];
        $list = DB::table('classodd')->where('cid','=',$_GET['cid'])->get();
        echo $list;
    }
    public function store(Request $request)
    {
        // dd($request);
        if($request->has('classvalue')){
            if ($request->input('classname')===0) {
                $data = array('classname'=>$request->input('classvalue'),'cid'=>0,'level'=>1);
                $m = DB::table('classodd')->insertGetId($data);
                   if ($m>0) {
                        return redirect('/admin/classone');
                    }else{
                        return back();
                    }
            }     
            if ($request->has('cid')===false && $request->input('classname')!=0) {
                $classname = $request->input('classvalue');
                $cid = $request->input('classname');
                $level = 2;
                $data = array('classname'=>$classname,'cid'=>$cid,'level'=>$level);
                $m = DB::table('classodd')->insertGetId($data);
                  if ($m>0) {
                       return redirect('/admin/classone');
                   }else{
                       return back();
                   }
            }
            if($request->input('classname')=='--请添加--'){
                $classname = $request->input('classvalue');
                $cid = $request->input('cid');
                $level = $request->input('level');
                $data = array('classname'=>$classname,'cid'=>$cid,'level'=>$level);
                $m = DB::table('classodd')->insertGetId($data);
                  if ($m>0) {
                       return redirect('/admin/classone');
                   }else{
                       return back();
                   }
            }
            if($request->input('classname')!='--请添加--'){
                $classname = $request->input('classvalue');
                $cid = $request->input('classname');
                $level = $request->input('level')+1;
                $data = array('classname'=>$classname,'cid'=>$cid,'level'=>$level);
                $m = DB::table('classodd')->insertGetId($data);
                  if ($m>0) {
                       return redirect('/admin/classone');
                   }else{
                       return back();
                   }
            }
        }else{
            return back()->with('msg','信息不能为空');
        }
    }
}
